package com.hangwei.aicabinet.presenter;

import com.hangwei.aicabinet.manager.NetManager;
import com.hangwei.aicabinet.model.bean.AllCabinetInfo;
import com.hangwei.aicabinet.net.ResponseInfo;
import com.hangwei.aicabinet.net.ViewCallBack;
import com.hangwei.aicabinet.view.BindView;

import rx.Observable;

/**
 * Created by Dell on 2018/9/8.
 */

public class BindPresenter extends BasePresenter<BindView> {

    public BindPresenter(BindView bindView) {
        super(bindView);
    }

    public void getAllCabinetInfo(final ViewCallBack viewCallBack) {
        if (viewCallBack != null) viewCallBack.onNetworkStart();
        NetManager.getInstance().doSubscribe(new NetManager.Model<AllCabinetInfo>() {
            @Override
            protected Observable<ResponseInfo<AllCabinetInfo>> getObservable() {
                return NetManager.getResponseInfoAPI().getAllCabinetInfo();
            }

            @Override
            protected void onNext(AllCabinetInfo data) {
                if (viewCallBack != null) viewCallBack.onNetworkComplete(true);
                mView.setCabinet(data);

            }

            @Override
            protected void onError() {
                if (viewCallBack != null) viewCallBack.onNetworkComplete(false);
            }
        });
    }

    public void initCabinet(final String id, final ViewCallBack viewCallBack) {
        if (viewCallBack!=null)
            viewCallBack.onNetworkStart();
        NetManager.getInstance().doSubscribe(new NetManager.Model<String>() {
            @Override
            protected Observable<ResponseInfo<String>> getObservable() {
                return NetManager.getResponseInfoAPI().initCabinet(id);
            }

            @Override
            protected void onNext(String data) {
                if (viewCallBack!=null)viewCallBack.onNetworkComplete(true);
                mView.bindSuccess(id);
            }

            @Override
            protected void onError() {
                if (viewCallBack!=null)viewCallBack.onNetworkComplete(false);
            }
        });
    }
}
